package lime.middleware;

/**
 * <p>Title: Lime</p>
 * <p>Description: Lime</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: DII - DISMI</p>
 * @author Marco Mamei
 * @version 1.0
 */


import java.util.*;

import emulator.utils.*;
import tuplespace.tuples.*;
import eventengine.middleware.*;

/* the lime tuple engine is the component in charge of monitoring tuples in and
out from the tuple space transfer and remove tuples from
the virtual tuple space accordingly. */

public class LimeTupleEngine implements ReactiveComponent {
 private Lime lime;
 public LimeTupleEngine(Lime lime)
 {
  this.lime = lime;
  System.out.println(lime.peer);
  /* set up call back reading */
  lime.peer.callBackReading(lime.LIME_SOCKET,"<LIME><WROTE>",this,"LIME_WROTE");
  lime.peer.callBackReading(lime.LIME_SOCKET,"<LIME><EXTRACTED>",this,"LIME_EXTRACTED");
 }

 public void wrote(Tuple t) {
  String flatT = t.serialize();
  if(! lime.isInBlackList(flatT))
  {
   String data = "<LIME><WROTE><"+flatT+">";
   lime.diffusionBuffer.store(t.id,data);
   //lime.peer.send(lime.LIME_SOCKET,data);
  }
 }

 public void extracted(Vector v) {
  String data = "<LIME><EXTRACTED>";
  for(int i=0;i<v.size();i++)
  {
   String flatT = ((Tuple)v.get(i)).serialize();
   if(! lime.isInBlackList(flatT))
    data = data + "<"+flatT+">";
  }

  if(data.length() > "<LIME><EXTRACTED>".length())
   lime.diffusionBuffer.store(data, data);
   //lime.peer.send(lime.LIME_SOCKET,data);   // send the message
                                            // only if tuples are actually sent

 }

 /*****************************************************************************/
 /*                          REACTIVE INTERFACE                               */
 /*****************************************************************************/
 public void react(String reaction, String event) {
  if(reaction.equalsIgnoreCase("LIME_WROTE"))
  {
   test("lime "+event);
   try{
   //System.err.println(lime.peer.getAddress()+": "+event);
   String[] data = emulator.utils.StaticUtilities.splitMessage(event,'<','>');
   Tuple t = Tuple.deserialize(data[2]);
   
   //System.err.println(lime.peer.getAddress()+": "+t.serialize());
   
   lime.vts.write(t);
   }catch(Exception e){System.out.println("---"+event);}
  }
  else if(reaction.equalsIgnoreCase("LIME_EXTRACTED"))
  {
   String[] data = emulator.utils.StaticUtilities.splitMessage(event,'<','>');
   for(int i=2;i<data.length;i++)
   {
    Tuple t = Tuple.deserialize(data[i]);
    lime.vts.keykeyextr(t);
   }
  }
 }
 
 /*****************************************************************************/
 /*                              TEST INTERFACE                               */
 /*****************************************************************************/
 
 
 private static final String PM = "P3";
 private static final int MIN_TIME = 305;
 
 private void test(String label) {
  if(lime.toString().equals(PM)) {
  int currentTime = getTime();
  if(currentTime < MIN_TIME) return;	
	System.out.println(currentTime+": "+label);
  }
 }
 
 
 private int getTime() {
  SensorTuple st = new SensorTuple("<sensor=clock><value=*>");
  st = (SensorTuple)lime.keyrd(st);
  int currentTime = Integer.parseInt(st.value);
  return currentTime;
 }
 
 
}